﻿Imports BUS
Imports DTO
Public Class frmDichVu
    Dim i As Integer = 0

    Private Sub frmDichVu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        load_DICHVU()
        khoa_data()
    End Sub
    Public Sub load_DICHVU()
        Dim db As New DataTable()
        Dim ds As New DICHVUBUS
        db = ds.load_DICHVU()
        luoi.DataSource = db
        luoi.[ReadOnly] = True
        ' luoi.Columns["PASS"].Visible = false;
        Dim i As Integer
        For i = 0 To db.Rows.Count - 1
            luoi.Rows(i).Cells(0).Value = (i + 1)

        Next i

        th_tendichvu.DataBindings.Clear()
        th_tendichvu.DataBindings.Add("Text", db, "TENDICHVU")

        th_dongia.DataBindings.Clear()
        th_dongia.DataBindings.Add("Text", db, "DONGIA")



    End Sub
    Dim MADICHVU_ As String
    Private Sub luoi_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles luoi.CellContentClick

        MADICHVU_ = luoi.CurrentRow.Cells("MADICHVU").Value.ToString
    End Sub
    Public Sub mo_data_them()
        xl.Enabled = True
        th_dongia.Text = ""
        th_tendichvu.Text = ""


        xl1.Visible = False
        xl2.Visible = True
    End Sub

    Public Sub mo_data_capnhat()
        xl.Enabled = True
        xl1.Visible = False
        xl2.Visible = True
    End Sub
    Public Sub khoa_data()

        xl.Enabled = False
        xl1.Visible = True
        xl2.Visible = False
        load_DICHVU()
    End Sub

    Private Sub but_them_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles but_them.Click
        i = 1
        mo_data_them()
    End Sub

    Private Sub but_capnhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles but_capnhat.Click
        i = 0
        mo_data_capnhat()
    End Sub

    Private Sub but_xoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles but_xoa.Click
        Dim dto As New DICHVUDTO()
        Dim bus As New DICHVUBUS()
        dto = getinfo()

        Dim dlr As New DialogResult()
        dlr = MessageBox.Show("Bạn có chắn là xóa mẫu tin này không?", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Information)
        If dlr = DialogResult.OK Then
            If bus.xoa_DICHVU(dto) = True Then
                MessageBox.Show("Xoa thanh cong")
                load_DICHVU()
            Else
                MessageBox.Show("Du lieu chua xoa duoc, vui long kiem tra lai")

            End If
        End If
    End Sub
    Public Function getinfo() As DICHVUDTO
        Dim test As New kiemtra()
        Dim dto As New DICHVUDTO()
        ' gan dta
        Dim tam As Boolean = False
        dto.MADICHVU = MADICHVU_

        If test.ktra_rong(th_tendichvu.Text) = True Then
            dto.TENDICHVU = th_tendichvu.Text
            tam = True
        Else
            MessageBox.Show("Chưa nhập tên món ăn")
            tam = False
        End If

        If test.ktra_rong(th_dongia.Text) = True Then
            dto.DONGIA = Single.Parse(th_dongia.Text.ToString())
            tam = True
        Else
            MessageBox.Show("Chưa nhập đơn giá")
            tam = False
        End If

       


        If tam = True Then

            Return dto

        End If

    End Function

    Private Sub but_luu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles but_luu.Click
        Dim dto As New DICHVUDTO()
        Dim bus As New DICHVUBUS()
        dto = getinfo()
        If i = 1 Then
            If bus.them_DICHVU(dto) = True Then
                MessageBox.Show("Them thanh cong")
                load_DICHVU()
                khoa_data()
            Else
                MessageBox.Show("Chua Them duoc")
            End If
        Else
            If bus.capnhat_DICHVU(dto) = True Then
                MessageBox.Show("Cap nhat thanh cong")
                load_DICHVU()
                khoa_data()
            Else
                MessageBox.Show("Chua cap nhat duoc")
            End If
        End If
    End Sub

    Private Sub but_kluu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles but_kluu.Click
        khoa_data()
        load_DICHVU()
    End Sub

    Private Sub th_dongia_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles th_dongia.KeyPress
        If Not [Char].IsDigit(e.KeyChar) AndAlso Not [Char].IsControl(e.KeyChar) Then
            e.Handled = True
        End If
    End Sub



    Private Sub but_thoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles but_thoat.Click
        Me.Close()
    End Sub
End Class